import { rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import react from '@vitejs/plugin-react'
import tsconfigPaths from 'vite-tsconfig-paths'
import { defineConfig } from 'vitest/config'
import { removeImportWasi, setEnvPlugin } from './vite-plugins/index.js'

const outDir = 'dist-cli/html'

// https://vite.dev/config/
export default defineConfig({
  base: './',
  build: {
    chunkSizeWarningLimit: 5000, // TODO: Reduce this value if possible
    emptyOutDir: true,
    outDir,
    rollupOptions: {
      plugins: [
        {
          // Excludes `schema.json` from the build output as it is generated by the CLI during `erd build`.
          // If `schema.json` exists in the ./public directory, it may be included by Vite, so this ensures it's removed.
          name: 'exclude-schema-json',
          closeBundle() {
            rmSync(`${outDir}/schema.json`, { force: true })
          },
        },
        {
          // Generate serve.json for the serve package
          name: 'generate-serve-json',
          closeBundle() {
            const serveConfig = {
              headers: [
                {
                  source: '**/*.@(html|json)',
                  headers: [
                    {
                      key: 'Cache-Control',
                      value: 'no-cache',
                    },
                  ],
                },
              ],
            }
            writeFileSync(
              join(outDir, 'serve.json'),
              JSON.stringify(serveConfig, null, 2),
            )
          },
        },
      ],
    },
  },
  plugins: [react(), tsconfigPaths(), removeImportWasi(), setEnvPlugin()],
  test: {
    globals: true,
    environment: 'node',
    coverage: {
      reporter: ['text', 'json', 'html'],
    },
  },
})
